package com.example.swimmingpool.util;

import cn.hutool.core.util.StrUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class OkHttpUtil {
    public static String get(String url, String... params) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(180, TimeUnit.SECONDS)
                .readTimeout(180, TimeUnit.SECONDS)
                .writeTimeout(180, TimeUnit.SECONDS)
                .build();

        String dataUrl = StrUtil.format(url, params[0], params[1], params[2]);
        Request request = new Request.Builder()
                .url(dataUrl)
                .get()
                .build();
        String result;
        try (Response response = client.newCall(request).execute()) {
            assert response.body() != null;
            result = response.body().string();
            return result;
        } catch (IOException e) {
            throw new IOException(e);
        }

    }

}
